﻿using System.IO;
using UnityEditor;
using UnityEngine;

namespace UPA
{
    [CustomPropertyDrawer(typeof(BuildPlayerActionArgument))]
    public class BuildPlayerActionArgumentDrawer:PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            property.serializedObject.Update();
            
            EditorGUILayout.PropertyField(property.FindPropertyRelative("appVersion"), new GUIContent("应用版本号"));
            EditorGUILayout.PropertyField(property.FindPropertyRelative("codeVersion"), new GUIContent("代码版本号"));
            
            GUILayout.BeginHorizontal();
            var exportRootPathProperty = property.FindPropertyRelative("exportRootPath");
            EditorGUILayout.PropertyField(exportRootPathProperty, new GUIContent("包体导出根路径"));
            if (GUILayout.Button("选择", GUILayout.ExpandWidth(false)))
            {
                var directory = string.IsNullOrWhiteSpace(exportRootPathProperty.stringValue)
                    ? Directory.GetCurrentDirectory()
                    : exportRootPathProperty.stringValue;
                var path = EditorUtility.OpenFolderPanel("选择导出根路径", directory,string.Empty);
                if (!string.IsNullOrWhiteSpace(path))
                {
                    exportRootPathProperty.stringValue = PathUtils.MakeRelativeTo(Directory.GetCurrentDirectory()+Path.DirectorySeparatorChar,path);
                    property.serializedObject.ApplyModifiedProperties();
                }
            }
            GUILayout.EndHorizontal();
            
            EditorGUILayout.PropertyField(property.FindPropertyRelative("profile"), new GUIContent("打包配置"));
            property.serializedObject.ApplyModifiedProperties();
        }
    }
}